package cj.web.admin.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.dao.SurveyDAO;
import cj.web.admin.dao.SurveyGroupDAO;
import cj.web.admin.dao.SurveyPeopleDAO;
import cj.web.admin.domain.Survey;
import cj.web.admin.domain.SurveyGroup;
import cj.web.admin.domain.SurveyPeople;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("surveyPeopleExport")
public class SurveyPeopleExportController extends BaseController {

	private static Logger logger = Logger
			.getLogger(SurveyPeopleExportController.class.getName());

	private final static int excelCount = 50000;

	@Autowired
	private SurveyDAO surveyDao;
	@Autowired
	private SurveyPeopleDAO surveyPeopleDao;
	@Autowired
	private SurveyGroupDAO surveyGroupDao;

	@Get("show")
	public String show(Model model) {
		model.add("MemuEnum", MemuEnum.lkgksrydc);
		List<SurveyGroup> objs = surveyGroupDao.queryAll();
		model.add("objs", objs);
		return "/views/survey/surveyPeopleExportList.vm";
	}

	@Get("output")
	public void output(@Param("surveyId") int surveyId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Survey obj = surveyDao.queryById(surveyId);

		String title = URLEncoder.encode(obj.getName(), "UTF-8"); // 进行转码，使其支持中文文件名
		response.setContentType("application/vnd.ms-excel"); // 生成提示信息
		response.setHeader("content-disposition", "attachment;filename="
				+ title + DateUtils.getNowTime(DateUtils.DATE_FULL_STR1)
				+ ".xls");
		OutputStream fOut = response.getOutputStream();

		// 参赛证总数
		int count = surveyPeopleDao.querysize(surveyId);
		double sheetSize = Double.parseDouble(String.valueOf(count)) / excelCount;
		int sheetCount = (int) Math.ceil(sheetSize);

		ExportExcel<SurveyPeople> ex = new ExportExcel<SurveyPeople>();
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();

		for (int i = 0; i < sheetCount; i++) {

			List<Map> dataset = null;

			// 信息Export
			String[] headers = { "名字", "单位", "手机", "成绩", "考试起", "考试止" };
			String[] fields = { "name", "companyGroup", "phone", "score", "createAt", "updateAt" };

			dataset = surveyPeopleDao.query(surveyId, i * excelCount,
					excelCount);

			ex.exportExcelMore(workbook,obj.getName() + "-" + i * excelCount, headers, fields,
					dataset, DateUtils.DATE_FULL_STR);
		}
		
		try {
			//关闭工作薄
			workbook.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		}

		fOut.flush();
		fOut.close();
	}

	
}